//
#include "stm8s.h"
#include "type.h"
#include "application.h"
#include "app_Task.h"
#include "RS485.h"

uint8 Serial_TaskID;					//系统串口通信任务ID


void Serial_Task_Init(uint8 task_id)
{
	Serial_TaskID = task_id;

	UART_Config();

}

//串口通信任务事件处理
uint16 RS485_Task_EventProcess(uint8 task_id,uint16 task_event)
{
	if ( task_event & SYS_EVENT_MSG )   	//判断系统消息事件
  	{
  		osal_sys_msg_t *MSGpkt;    			//定义一个指向接受系统消息结构体的指针

	    //从消息队列获取消息  
	    MSGpkt = (osal_sys_msg_t *)osal_msg_receive( task_id ); 
		while ( MSGpkt )
	    {
	      	switch ( MSGpkt->hdr.event )  	//判断消息事件
	      	{
	          	case OSAL_PRINTF:
					RS485_lamp();
	          		break;

	        	default:
	          		break;
	      	}

	      	// Release the memory
	      	osal_msg_deallocate( (uint8 *)MSGpkt );

	      	// Next  获取下一个消息
	      	MSGpkt = (osal_sys_msg_t *)osal_msg_receive( task_id );
	    }

	return (task_event ^ SYS_EVENT_MSG);
	}

return 0;

}

uint8 RS485_Sendstate ( uint8 keys, uint8 state )
{
    osal_sys_msg_t* msgPtr;

    if ( registeredKeysTaskID != NO_TASK_ID )
    {
        // Send the address to the task
        msgPtr = ( osal_sys_msg_t* ) osal_msg_allocate ( sizeof ( osal_sys_msg_t ) );
        if ( msgPtr )
        {
            msgPtr->hdr.event = 1;
            msgPtr->hdr.status = state;
            msgPtr->hdr.event = 0;
             

            osal_msg_send ( registeredKeysTaskID, ( uint8* ) msgPtr );
        }
        return ( SUCCESS );
    }
    else
    {
        return ( FALSE );
    }
}